package com.lubase.metadata.mapper;

import com.lubase.metadata.model.ColumnMetaModel;
import com.lubase.metadata.model.ConstraintMetaModel;
import com.lubase.metadata.model.TableMetaModel;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 多数据库测试mapper
 */
@Mapper
public interface MysqlMetadataReadMapper {
    @Select("SELECT table_name,table_comment FROM   information_schema.tables WHERE  table_schema='${databaseCode}' AND table_type='BASE TABLE'")
    @Results({
            @Result(property = "tableName", column = "table_name"),
            @Result(property = "tableComment", column = "table_comment")
    })
    List<TableMetaModel> getTableList(String databaseCode);


    @Select("SELECT table_name,column_name,column_comment,column_type FROM   information_schema.columns WHERE table_schema='${databaseCode}'")
    @Results({
            @Result(property = "tableName", column = "table_name"),
            @Result(property = "columnName", column = "column_name"),
            @Result(property = "columnComment", column = "column_comment"),
            @Result(property = "columnType", column = "column_type")
    })
    List<ColumnMetaModel> getTableColumnListByDatabase(@Param("databaseCode") String databaseCode);


    @Select("SELECT  * FROM  information_schema.KEY_COLUMN_USAGE WHERE   referenced_table_name IS NOT null and  table_schema='${databaseCode}'")
    @Results({
            @Result(property = "constraintName", column = "constraint_name"),
            @Result(property = "tableName", column = "table_name"),
            @Result(property = "columnName", column = "column_name"),
            @Result(property = "referencedTableName", column = "referenced_table_name"),
            @Result(property = "referencedColumnName", column = "referenced_column_name")
    })
    List<ConstraintMetaModel> getTableConstraintList(@Param("databaseCode") String databaseCode);
}
